package com.zdlee.netty.server17.client.console;

import com.zdlee.netty.server17.util.SessionUtil;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author zdlee
 * @description: ${DESCRIPTION}
 * @create 2018-12-01 13:30
 */
public class ConsoleCommandManager implements  ConsoleCommand{
   private Map<String,ConsoleCommand> consoleCommandMap;

   public ConsoleCommandManager() {
       consoleCommandMap=new HashMap<>();
       consoleCommandMap.put("sendToUser", new SendToUserConsoleCommand());
       consoleCommandMap.put("logout", new LogoutConsoleCommand());
       consoleCommandMap.put("createGroup", new CreateGroupConsoleCommand());
       consoleCommandMap.put("joinGroup", new JoinGroupConsoleCommand());
       consoleCommandMap.put("quitGroup", new QuitGroupConsoleCommand());
       consoleCommandMap.put("listGroupMembers", new ListGroupMembersConsoleCommand());
   }

    @Override
    public void exec(Scanner sc, Channel channel) {
       //获取第一个指令
       String command=sc.next();
       if(!SessionUtil.hasLogin(channel))
       {
           return;
       }
       ConsoleCommand consoleCommand=consoleCommandMap.get(command);
       if(consoleCommand!=null)
       {
           consoleCommand.exec(sc,channel);
       }else
       {
           System.err.println("无法识别[" + command + "]指令，请重新输入!");
       }

    }
}
